import Vue from 'vue'
import Router from 'vue-router'

import Layout from '@/components/layout/Layout.vue'
import baseBusinessRoute from './router.baseBusiness'
import systemConfigRoute from './router.systemConfig'
import store from '@/store/store'
import * as types from '@/store/mutation-types'

Vue.use(Router)

const routes = [
  {
    path: '/login',
    name: 'login',
    component: () => import(/* webpackChunkName: "login" */ '@/views/login/Login'),
    hidden: true
  },
  {
    path: '/',
    redirect: '/baseBusiness',
    hidden: true,
    meta: { requireAuth: true }
  },
  baseBusinessRoute,
  {
    path: '/digitalGrid',
    name: 'digitalGrid',
    component: Layout,
    redirect: '/digitalGrid/subGrid',
    meta: { title: '数字化网格', requireAuth: true },
    children: [
      {
        path: 'subGrid',
        name: 'subGrid',
        component: () => import(/* webpackChunkName: "digitalGrid" */ '@/views/digitalGrid/subGrid/SubGrid'),
        meta: { title: '网格一', requireAuth: true }
      }
    ]
  },
  {
    path: '/ddd',
    name: 'ddd',
    component: Layout,
    // redirect: '/digitalGrid/subGrid',
    meta: { title: '综治视联网', requireAuth: true },
    children: []
  },
  {
    path: '/eee',
    name: 'eee',
    component: Layout,
    // redirect: '/digitalGrid/subGrid',
    meta: { title: '应急指挥', requireAuth: true },
    children: []
  },
  {
    path: '/rrr',
    name: 'rrr',
    component: Layout,
    // redirect: '/digitalGrid/subGrid',
    meta: { title: '安监消防', requireAuth: true },
    children: []
  },
  {
    path: '/ttt',
    name: 'ttt',
    component: Layout,
    // redirect: '/digitalGrid/subGrid',
    meta: { title: '协同办公', requireAuth: true },
    children: []
  },
  {
    path: '/yyy',
    name: 'yyy',
    component: Layout,
    // redirect: '/digitalGrid/subGrid',
    meta: { title: '智慧党建', requireAuth: true },
    children: []
  },
  {
    path: '/uuu',
    name: 'uuu',
    component: Layout,
    // redirect: '/digitalGrid/subGrid',
    meta: { title: '综治考核', requireAuth: true },
    children: []
  },
  {
    path: '/iii',
    name: 'iii',
    component: Layout,
    // redirect: '/digitalGrid/subGrid',
    meta: { title: '大数据分析研判', requireAuth: true },
    children: []
  },
  systemConfigRoute
]

const routerObj = new Router({
  // mode: 'history',
  base: process.env.BASE_URL,
  routes
})
// 判断是否需要登录权限 以及是否登录
routerObj.beforeEach((to, from, next) => {
  // 从cookie 中获取token
  let reg = new RegExp('(^| )user=([^;]*)(;|$)')

  let token = document.cookie.match(reg)

  console.log(to, token)
  if (to.meta.requireAuth) {
    // 判断该路由是否需要登录权限
    if (token) {
      // 判断当前的token 是否有效
      if (Object.keys(from.query).length === 0) {
        next()
      } else {
        let redirect = from.query.redirect // 如果来源路由有query
        if (to.path === redirect) { // 这行是解决next无限循环的问题
          next()
        } else {
          next({ path: redirect }) // 跳转到目的路由
        }
      }
    } else {
      if (to.path === '/login') {
        next()
      } else {
        store.commit(types.DEL_USER) // 清除用户信息
        next({
          path: '/login',
          query: { redirect: to.fullPath } // 将跳转的路由path作为参数，登录成功后跳转到该路由
        })
      }
    }
  } else {
    next()
  }
})

export default routerObj

// {
//   path: '/about',
//   name: 'about',
//   // route level code-splitting
//   // this generates a separate chunk (about.[hash].js) for this route
//   // which is lazy-loaded when the route is visited.
//   component: () => import(/* webpackChunkName: "about" */ './views/About.vue')
// }
